package com.example.myone.Mapper;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.myone.Entity.Music;
import com.example.myone.Entity.UserArticleLike;
import com.example.myone.R;
import com.example.myone.Util.BytesUtils;
import com.example.myone.sql.MySQLiteHelper;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class UserArticleLikeMapper {
    private static final String TABLE_NAME = "user_article_like";

    // 插入一个用户点赞记录
    public static UserArticleLike insertUserArticleLike(Context context, UserArticleLike userArticleLike) {
        SQLiteDatabase db = MySQLiteHelper.getWritableDatabase(context);
        ContentValues values = new ContentValues();
        values.put("user_id", userArticleLike.userId);
        values.put("article_id", userArticleLike.articleId);
        long insert = db.insert(TABLE_NAME, null, values);
        userArticleLike.id = (int) insert;
        return userArticleLike;
    }

    // 根据用户ID和文章ID，获取用户是否点赞
    public static Boolean checkLikedByUserIdAndArticleId(Context context, UserArticleLike userArticleLike) {
        SQLiteDatabase database = MySQLiteHelper.getReadableSQLiteDatabase(context);
        Cursor cursor = database.rawQuery(
                "select * from user_article_like where user_id = ? and article_id = ?",
                new String[]{String.valueOf(userArticleLike.userId), String.valueOf(userArticleLike.articleId)});
        return getUserArticleLikes(cursor).size() > 0;
    }

    // 根据用户ID和文章ID，删除用户点赞记录
    public static int deleteByUserIdAndArticleId(Context context, UserArticleLike userArticleLike) {
        SQLiteDatabase db = MySQLiteHelper.getWritableDatabase(context);
        return db.delete(TABLE_NAME, "article_id = ? and user_id = ?",
                new String[]{String.valueOf(userArticleLike.articleId), String.valueOf(userArticleLike.userId)});
    }

    // 收集数据中的文章Id
    public static List<Integer> collectArticleIds(ArrayList<UserArticleLike> list) {
        return list.stream()
                .map(UserArticleLike::getArticleId)
                .collect(Collectors.toList());
    }


    // 获取点赞记录的辅助方法
    @SuppressLint("Range")
    public static ArrayList<UserArticleLike> getUserArticleLikes(Cursor cursor) {
        ArrayList<UserArticleLike> userArticleLikes = new ArrayList<>();
        if (cursor != null && cursor.moveToFirst()) {
            do {
                UserArticleLike userArticleLike = new UserArticleLike();
                userArticleLike.id = cursor.getInt(cursor.getColumnIndex("id"));
                userArticleLike.userId = cursor.getInt(cursor.getColumnIndex("user_id"));
                userArticleLike.articleId = cursor.getInt(cursor.getColumnIndex("article_id"));
                userArticleLikes.add(userArticleLike);
            } while (cursor.moveToNext());
            cursor.close();
        }
        return userArticleLikes;
    }

    // 检查表是否有数据
    private static boolean isTableEmpty(Context context) {
        SQLiteDatabase database = MySQLiteHelper.getReadableSQLiteDatabase(context);
        try (Cursor cursor = database.rawQuery("select count(*) from " + TABLE_NAME, null)) {
            cursor.moveToFirst();
            return cursor.getInt(0) == 0;
        }
    }
    // 初始化方法
    public static void init(Context context) {
        if (!isTableEmpty(context)) return;
        System.out.printf("==> 初始化 %s 数据库数据", TABLE_NAME);
        UserArticleLike userArticleLike = new UserArticleLike();
        userArticleLike.userId = 1;
        userArticleLike.articleId = 1;
        UserArticleLikeMapper.insertUserArticleLike(context, userArticleLike);
        userArticleLike.articleId = 4;
        UserArticleLikeMapper.insertUserArticleLike(context, userArticleLike);
        userArticleLike.articleId = 5;
        UserArticleLikeMapper.insertUserArticleLike(context, userArticleLike);
    }
}
